package com.atguigu.yygh.email.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.email.service.EmailService;
import com.atguigu.yygh.email.utils.ConstantEmailUtils;

import javax.annotation.Resource;

import com.atguigu.yygh.model.vo.msm.MsmVo;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

@Service
public class EmailServiceImpl implements EmailService {

    @Resource
    JavaMailSenderImpl javaMailSender;

    @Override
    @Async
    public void send(String email, String code) {
        SimpleMailMessage message = new SimpleMailMessage();
        //邮件设置
        message.setSubject("尚医通登录验证码");
        message.setText("尊敬的：" + email + "用户，欢迎您！您的验证码为：" + code + "。两分钟有效，请妥善保管！");
        message.setTo(email);
        message.setFrom(ConstantEmailUtils.SEND_USER);
        javaMailSender.send(message);
    }

    @Override
    @Async
    public void send(String email, Map<String, Object> param) {
        SimpleMailMessage message = new SimpleMailMessage();
        if (param.get("order").equals(1)) {
            //保存挂号订单
            message.setSubject("尚医通预约挂号");
            message.setText("尊敬的：" + param.get("name") + "用户，您好！您已完成预约挂号，挂号单号为：" + param.get("orderNo") + "。请尽快完成付款，谢谢！");
        } else if (param.get("order").equals(2)) {
            message.setSubject("尚医通取消挂号");
            message.setText("尊敬的：" + param.get("name") + "用户，您好！您已取消预约挂号，挂号单号为：" + param.get("orderNo") + "。");
        }
        //邮件设置
        message.setTo(email);
        message.setFrom(ConstantEmailUtils.SEND_USER);
        javaMailSender.send(message);
    }

    /**
     * mq发送
     *
     * @param msmVo msmVo
     */
    @Override
    public void send(MsmVo msmVo) {
        if (!StringUtils.isEmpty(msmVo.getPhone())) {
            this.send(msmVo.getPhone(), msmVo.getParam());
        }
    }


}
